package com.sri.business.property;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.GregorianCalendar;

import com.sri.cache.CacheManager;
import com.sri.db.dao.property.PropertyDAO;
import com.sri.db.dao.property.PropertyDAOServiceFactory;
import com.sri.formBeans.PropertyForm;
import com.sri.model.home.Home;

public class PropertyManager {
	public static void storePropery(PropertyForm propertyForm) {
		Home home = populateHome(propertyForm);
		PropertyDAO propertyDAO = PropertyDAOServiceFactory.getPropertyDAO();
		try {
			propertyDAO.storeHome(home);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static Home populateHome(PropertyForm form) {
		Home home = new Home();
		home.setName(form.getName());
		home.setRent(form.getRent());
		home.setZipCode(form.getZipCode());
		home.setCityId(Integer.parseInt(form.getCity()));
		home.setCityName(CacheManager.getCityName(Integer.parseInt(form
				.getCity())));
		home.setCreatedDate(new Timestamp(new GregorianCalendar()
				.getTimeInMillis()));
		home.setDisplay(1);
		home.setMaintenanceCost(form.getMaintenanceCost());
		home.setMessage(form.getMessage());
		home.setNoOfMembersAllowed(form.getNoOfMembersAllowed());
		home.setOwnerId(1);
		home.setRentNegotiable(form.isRentNegotiable());
		home.setStateId(Integer.parseInt(form.getState()));
		home.setStateName(CacheManager.getStateName(Integer.parseInt(form
				.getState())));
		home.setTypeId(1);
		return home;
	}
}
